package com.paomi.client.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;

import com.paomi.client.view.banner.xbanner.XBanner;


public class HorizontalNewBanner extends XBanner {
    private ViewGroup mPtrLayout;
    private float mDownX;
    private float mDownY;

    public HorizontalNewBanner(Context context) {
        super(context);
    }

    public HorizontalNewBanner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HorizontalNewBanner(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public void setNestedpParent(ViewGroup parent) {
        this.mPtrLayout = parent;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                getParent().requestDisallowInterceptTouchEvent(true);
                if (mPtrLayout != null) {
                    mPtrLayout.setEnabled(false);
                }

                break;
            case MotionEvent.ACTION_MOVE:
                int moveX = (int) ev.getX();
                int moveY = (int) ev.getY();
                //ViewPager滑动
                if (Math.abs(moveX - mDownX) > Math.abs(moveY - mDownY)) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    if (mPtrLayout != null) {
                        mPtrLayout.setEnabled(false);
                    }
                    //PtrFrameLayout滑动
                } else {
                    getParent().requestDisallowInterceptTouchEvent(false);
                    if (mPtrLayout != null) {
                        mPtrLayout.setEnabled(true);
                    }
                }
                mDownX = moveX;
                mDownY = moveY;
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                if (mPtrLayout != null) {
                    mPtrLayout.setEnabled(true);
                }
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

}